package MyThread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: AliceMana
 * Date: 2023-01-01
 * Time: 15:07
 */
public class ThreadDemo14 {
    public static void main(String[] args) {
        Object locker1 = new Object();
        Object locker2 = new Object();

        // 假定locker1 为 1 号, locker2 为 2 号, 约定先拿小的, 后拿大的
        Thread t1 = new Thread(() -> {
            synchronized (locker1) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (locker2) {
                    System.out.println("t1获取到了两把锁");
                }
            }
        });
        Thread t2 = new Thread(() -> {
            synchronized (locker1) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (locker2) {
                    System.out.println("t2获取到了两把锁");
                }
            }
        });
        t1.start();
        t2.start();
    }
}
